<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <input type="button" class="login" value="get的登录">
  <input type="button" class="register" value="post的注册">
</body>
<script>
  // const url = "http://10.11.55.86:3000/login?username=admin&password=123456";

  // console.log(fetch);

  // fetch(url).then(res=>{
  //   return res.json()
  // }).then(res=>{
  //   console.log(res);
  // })

  // console.log(1)
  // fetch(url).then(res=>res.text()).then(res=>{
  //   console.log(res);
  // }).catch(res=>{
  //   console.log(res,1);
  // })
  // console.log(2)

  // 使用fetch发起get请求
  document.querySelector('.login').onclick = function(){
    let url = "http://10.11.55.86:3000/login";

    const data = {
      username:"admin",
      password:123456
    }

    let str = ""
    for(let i in data){
      str += `${i}=${data[i]}&`;
    }

    url += "?" + str.slice(0,-1);

    // get的请求
    fetch(url, {
      method:"GET"
    }).then(res=>res.json()).then(res=>{
      console.log(res);
    })
  }


  // 使用fetch发起post请求
  document.querySelector('.register').onclick = function(){
    const url = "http://10.11.55.86:3000/adduser";

    const data = {
      token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1SWQiOiJ1c2VyLTFmOGM1NGYwLTBlN2YtMTFlZS1iMDJiLTJkODY3YjMzOTQ5ZCIsInVzZXJuYW1lIjoiYWRtaW4iLCJwb3dlciI6MCwiaWF0IjoxNjg4MTA2MDExLCJleHAiOjE2ODgxMDk2MTF9.plI2G5wR5PezQKDxJ-XHULvm-hgS36F9pN30Rnp4FcM",
      uName:"张三",
      tel:"17600902005"
    }

    let str = "";
    for(let i in data){
      str += `${i}=${data[i]}&`;
    }
  
    fetch(url,{
      method: "POST",
      body: str.slice(0,-1),
      headers: {
        "content-type" : "application/x-www-form-urlencoded"
      }
    }).then(res=>res.json()).then(res=>{
      console.log(res);
    })
  }

  
</script>
</html>